package projectsupporter.common.element;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;

import projectsupporter.common.constant.Constant;
import projectsupporter.common.control.Path;
import projectsupporter.common.formbase.FolderFromWorkspaceDialog;
import projectsupporter.common.logic.PlugLogic;
import projectsupporter.common.operation.CreateBaseCodeOperation;
import projectsupporter.common.operation.LoadTableInfoOperation;
import projectsupporter.form.MainForm;

import dbTools.DBToolEngine;
import dbTools.XML.ValidatorXMLService;
import dbTools.XML.XMLService;
import dbTools.coeModel.CodeModelCreator;
import dbTools.pojo.JavaBeanCreator;
import dbTools.tableInfo.info.TableInfo;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

public class AddBaseCodeCompsite extends Composite {

	private Map<String,TableInfo> tableInfos;
	
	private Table table = null;

	private Label label = null;

	private Button button = null;

	private Button hibMapchb = null;

	private Text hibMapPath = null;

	private Label label1 = null;

	private Label label2 = null;

	private Button button1 = null;

	private Button pojochb = null;

	private Label label11 = null;

	private Text pojoPath = null;

	private Button button11 = null;

	private Label label21 = null;

	private Button validatechb = null;

	private Label label111 = null;

	private Text validatePath = null;

	private Button button111 = null;

	private Label label211 = null;
	
	private Path abHibPath;
	private Path abPojoPath;
	private Path abValiPath;
	private Path abSqlPath;

	private Button floderByIDcbx = null;

	private Shell parentShell;

	private Button sqlchb = null;

	private Label label1111 = null;

	private Text sqlPath = null;

	private Button button1111 = null;

	private Label label2111 = null;

	public AddBaseCodeCompsite(Composite parent, int style,Shell parentShell) {
		super(parent, style);
		initialize();
		this.parentShell=parentShell;
		tableInfos= new LinkedHashMap<String,TableInfo>();
	}

	private void initialize() {
		table = new Table(this, SWT.MULTI | SWT.BORDER);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setBounds(new Rectangle(8, 6, 104, 380));
		label = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		label.setBounds(new Rectangle(0, 386, 720, 18));
		label.setText("Label");
		button = new Button(this, SWT.NONE);
		button.setBounds(new Rectangle(597, 411, 109, 28));
		button.setText("Run");
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				
				 try {
						new ProgressMonitorDialog(parentShell).run(true, true,
						        new CreateBaseCodeOperation(tableInfos,floderByIDcbx.getSelection(),abHibPath,abPojoPath,abValiPath,abSqlPath));
					} catch (InvocationTargetException e1) {
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			}
		});
		hibMapchb = new Button(this, SWT.CHECK | SWT.RIGHT);
		hibMapchb.setSize(new Point(207, 18));
		hibMapchb.setSelection(true);
		hibMapchb.setText("Create Hibernate Mapping Files");
		hibMapchb.setLocation(new Point(146, 11));
		hibMapchb.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if(hibMapchb.getSelection())
				{
					hibMapPath.setEnabled(true);
					button1.setEnabled(true);
				}
				else
				{
					hibMapPath.setEnabled(false);
					button1.setEnabled(false);
				}
			}
		});
		hibMapPath = new Text(this, SWT.BORDER);
		hibMapPath.setSize(new Point(449, 24));
		hibMapPath.setLocation(new Point(171, 35));
		label1 = new Label(this, SWT.NONE);
		label1.setText("Path :");
		label1.setLocation(new Point(124, 38));
		label1.setSize(new Point(40, 18));
		label2 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label2.setBounds(new Rectangle(120, 61, 585, 18));
		label2.setText("Label");
		button1 = new Button(this, SWT.NONE);
		button1.setSize(new Point(82, 28));
		button1.setText("Browse");
		button1.setLocation(new Point(624, 32));
		button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				abHibPath=browsePath(hibMapPath);
			}
		});
		pojochb = new Button(this, SWT.CHECK | SWT.RIGHT);
		pojochb.setSize(new Point(122, 18));
		pojochb.setSelection(true);
		pojochb.setText("Create Pojo Files");
		pojochb.setLocation(new Point(149, 95));
		pojochb.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if(pojochb.getSelection())
				{
					pojoPath.setEnabled(true);
					button11.setEnabled(true);
				}
				else
				{
					pojoPath.setEnabled(false);
					button11.setEnabled(false);
				}
				
			}
		});
		label11 = new Label(this, SWT.NONE);
		label11.setText("Path :");
		label11.setLocation(new Point(127, 122));
		label11.setSize(new Point(41, 18));
		pojoPath = new Text(this, SWT.BORDER);
		pojoPath.setLocation(new Point(174, 119));
		pojoPath.setSize(new Point(449, 24));
		button11 = new Button(this, SWT.NONE);
		button11.setText("Browse");
		button11.setSize(new Point(82, 28));
		button11.setLocation(new Point(627, 116));
		button11.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				abPojoPath=browsePath(pojoPath);
			}
		});
		label21 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label21.setText("Label");
		label21.setSize(new Point(585, 18));
		label21.setLocation(new Point(123, 145));
		validatechb = new Button(this, SWT.CHECK | SWT.RIGHT);
		validatechb.setSize(new Point(152, 18));
		validatechb.setSelection(true);
		validatechb.setText("Create Validation Files");
		validatechb.setLocation(new Point(148, 186));
		validatechb.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if(validatechb.getSelection())
				{
					validatePath.setEnabled(true);
					button111.setEnabled(true);
				}
				else
				{
					validatePath.setEnabled(false);
					button111.setEnabled(false);
				}
			}
		});
		label111 = new Label(this, SWT.NONE);
		label111.setText("Path :");
		label111.setLocation(new Point(126, 213));
		label111.setSize(new Point(40, 18));
		validatePath = new Text(this, SWT.BORDER);
		validatePath.setSize(new Point(449, 24));
		validatePath.setLocation(new Point(173, 210));
		button111 = new Button(this, SWT.NONE);
		button111.setText("Browse");
		button111.setLocation(new Point(626, 207));
		button111.setSize(new Point(82, 28));
		button111.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				abValiPath=browsePath(validatePath);
			}
		});
		label211 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label211.setText("Label");
		label211.setSize(new Point(585, 18));
		label211.setLocation(new Point(122, 236));
		floderByIDcbx = new Button(this, SWT.CHECK);
		floderByIDcbx.setBounds(new Rectangle(146, 346, 118, 18));
		floderByIDcbx.setSelection(true);
		floderByIDcbx.setText("Foleder By Id");
		sqlchb = new Button(this, SWT.CHECK | SWT.RIGHT);
		sqlchb.setBounds(new Rectangle(157, 272, 125, 18));
		sqlchb.setText("Create SQL Script");
		sqlchb.setSelection(true);
		label1111 = new Label(this, SWT.NONE);
		label1111.setBounds(new Rectangle(130, 299, 35, 18));
		label1111.setText("Path :");
		sqlPath = new Text(this, SWT.BORDER);
		sqlPath.setBounds(new Rectangle(174, 295, 445, 24));
		button1111 = new Button(this, SWT.NONE);
		button1111.setBounds(new Rectangle(626, 291, 79, 28));
		button1111.setText("Browse");
		button1111.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				abSqlPath=browsePath(sqlPath);
			}
		});
		
		label2111 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label2111.setBounds(new Rectangle(122, 326, 587, 10));
		label2111.setText("Label");
		table.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
				  TableItem[] items=table.getSelection();
				  if(items!=null && items.length>0 && e.button == 3)
				  {
					  showMenu();
				  }
			}
		});
		TableColumn col1 = new TableColumn(table, SWT.LEFT);
		col1.setText("TableName");  
		col1.setWidth(table.getSize().x);  
		TableColumn col2 = new TableColumn(table, SWT.LEFT);
		col2.setText("TableId");  
		col2.setWidth(0);
		col2.setResizable(false);
		
		this.setSize(new Point(720, 449));
		
		//table.setSize(table.getSize().x, this.getSize().y);
		setLayout(null);
	}
	
	public void addSelectedItem(TreeItem[] items)
	{
		for(TreeItem item : items)
		{
			if(item.getData(Constant.ITEM_TYPE).equals(Constant.ITEM_TYPE_TABLE)==false)
			{
				continue;
			}
			
			TableInfo tableInfo=DBToolEngine.TABLE_INFO_MAP.get(item.getData(Constant.TABLE_KEY).toString());
			int itemIndex= getExistedItemIndex(tableInfo.getTableName());
			
			if(itemIndex==-1)
			{
				new TableItem(table, SWT.LEFT).setText(new String[] {tableInfo.getTableName(),tableInfo.getKey()});
				this.tableInfos.put(tableInfo.getKey(), tableInfo);
			}		
		}
		System.out.println("sus");
	}
	
	public void removeSelectedItem(TableItem[] items)
	{
		for(TableItem item : items)
		{
			TableInfo tableInfo=DBToolEngine.TABLE_INFO_MAP.get(item.getText(1));
			int itemIndex= getExistedItemIndex(tableInfo.getTableName());
			
			if(itemIndex >= 0)
			{
				table.remove(itemIndex);
				this.tableInfos.remove(tableInfo.getKey());
			}		
		}
		 
	}
	
	private int getExistedItemIndex(String tableName)
	{
		if(table.getItems()==null)
		{
			return -1;
		}
		
		for(int i= 0;i<table.getItemCount();i++)
		{
			TableItem item = table.getItem(i);
			if(item.getText(0).equals(tableName))
			{
				return i;
			}
		}
		
		return -1;
	}
	
	private void showMenu() {
		Menu tmpMenu = new Menu(this);         //增加右键菜单
        tmpMenu.setVisible(true);              //menu必须设置成visible才能显示出 
        setAddBaseCodeMenuItem(tmpMenu);
	}
	
	private void setAddBaseCodeMenuItem(Menu tmpMenu)
	{
		 MenuItem tmpItem = new MenuItem(tmpMenu, SWT.PUSH);
         tmpItem.setText("RemoveTable");          
         tmpItem.addSelectionListener(new SelectionListener() {
             
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				System.out.println("DefaultSelected");
				
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				removeSelectedItem(table.getSelection());
				System.out.println("Selected");
			}
         });
	}
	
	private Path browsePath(Text text)
	{
		String displayPath="";
	
		FolderFromWorkspaceDialog folderdlg=new FolderFromWorkspaceDialog(this.getShell());
		if(text.getText()!=null)
		{
			folderdlg.setDefaultFolder(text.getText());
		}
		Path abPath=folderdlg.openFolderSelectDialog();

        if(abPath!=null)
        {
        	displayPath=abPath.getDisplayPath();
        	text.setText(displayPath);
        }
        else
        {
        	return null;
        }
        
        if(hibMapPath.getText()==null || hibMapPath.getText().trim().length()==0)
        {
        	abHibPath=abPath;
        	hibMapPath.setText(displayPath);
        }
        if(pojoPath.getText()==null || pojoPath.getText().trim().length()==0)
        {
        	abPojoPath=abPath;
        	pojoPath.setText(displayPath);
        }
        if(validatePath.getText()==null || validatePath.getText().trim().length()==0)
        {
        	abValiPath=abPath;
        	validatePath.setText(displayPath);
        }
        
        return abPath;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
